# -*- coding: utf-8 -*-
#
# Copyright (c) 2012, Rafael Pivato
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Rafael Pivato nor the names of its contributors
#       may be used to endorse or promote products derived from this
#       software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL RAFAEL PIVATO BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import logging
import webapp2

from functools import wraps
from google.appengine.api import memcache
from google.appengine.api import users
from wowgae.model import Guild


class memcache_property(object):

    def __init__(self, fget):
        self.__name__ = fget.__name__
        self.__module__ = fget.__module__
        self.__doc__ = fget.__doc__
        self.fget = fget
        self.fval = None

    def __get__(self, instance, owner):
        if instance is None:
            logging.info('returning right here')
            return self
        logging.info('going ahead')
        namespace = self.__module__
        key = self.__name__
        value = memcache.get(key, namespace, False)
        if value is None:
            value = self.fget(instance)
            memcache.set(key, value, namespace=namespace)
        return value

    def __set__(self, instance, value):
        if instance is None:
            logging.info('returning right here also')
            return self
        logging.info('setting ahead')
        namespace = self.__module__
        key = self.__name__
        memcache.set(key, value, namespace=namespace)


def login_required(method):
    @wraps(method)
    def wrapper(self, *args, **kwds):
        current = users.get_current_user()
        if current is None:
            url = self.request.url
            login = users.create_login_url(url)
            self.redirect(login)
        else:
            method(self, *args, **kwds)
    return wrapper


def membership_required(method):
    @wraps(method)
    def wrapper(self, *args, **kwds):
        current_user = self.current_user
        if current_user:
            current_account = self.current_account
            if current_account:
                members_since = Guild.members_since()
                if current_account.locked:
                    logging.warn('Locked account %s (%s) identified.' % \
                                 (current_account.email,
                                  current_account.name))
                    self.abort(403, detail="Account Locked.",
                               comment="Please, refer to your guild leader.")
                elif current_account.member_until and \
                current_account.member_until < members_since:
                    logging.warn('Deserter account %s (%s) identified.' % \
                                 (current_account.email,
                                  current_account.name))
                    self.abort(403, detail="Account Locked.",
                               comment="Please, refer to your guild leader.")
                else:
                    method(self, *args, **kwds)
            else:
                uri = webapp2.uri_for('activation')
                self.redirect(uri)
        else:
            url = self.request.url
            login = users.create_login_url(url)
            self.redirect(login)
    return wrapper
